<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>时钟</title>
</head>

<body onload="clockTime()">
    <script>
        // document.write("aaa");
        // document.write("bbb");
        // document.writeln("ccc");
        // document.writeln("ddd");

        function clockTime() {
            // 获取当前时间
            var todayDate = new Date();
            var year = todayDate.getFullYear(); //年份
            var month = todayDate.getMonth() + 1; //月份
            var date = todayDate.getDate(); //日期
            var hour = todayDate.getHours();
            var minute = todayDate.getMinutes();
            var second = todayDate.getSeconds();
            // 2021-10-20 8:40:20
            var time = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;

            //1 得到div元素
            var obj = document.getElementById("displayTime");
            // 2 给元素写html或文本，innerHTML 设置或返回标签之间的HTML
            obj.innerHTML = time;

            setTimeout('clockTime()', 1000);

        }
    </script>
    <div id="displayTime"> </div>

</body>

</html>